/*
   A helper class that passes clicks thru
   Created by Randall Maas, February 22 2009
   Copyright (c) 2009, Randall Maas
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:

   Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.

   Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

   Neither the name of Blackwood Designs, LLC nor the names of its contributors
   may be used to endorse or promote products derived from this software without
   specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
   THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Windows.Forms;
using System.Drawing;

public partial class UnfocusableForm : Form
{
   #region Bits that customize its look
   public UnfocusableForm(bool X) : base()
   {
      if (X)
        {
           ControlBox = false;
           FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
           ShowInTaskbar = false;
           ShowIcon = false;
           MinimizeBox = false;
           SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
           StartPosition = System.Windows.Forms.FormStartPosition.Manual;
        }
   }

   /// <summary>
   /// Set to 0x20 to make transparent to mouse clicks
   /// </summary>
   protected  int ExStyle = 0;
   protected override CreateParams CreateParams
   {
      get
      {
         CreateParams cp=base.CreateParams;
         cp . ExStyle |= ExStyle;
         return cp;
      }
   }
   #endregion


   /// <summary>
   /// This is used to make the window the size of the primary screen
   /// </summary>
   protected void FullScreen()
   {
      WindowState = FormWindowState.Normal;
      KeyPreview = false;
      Rectangle fullSize = Program . ScreenRectangle();

      // cover the main monitors with one gigantic window
      Location   = new Point(fullSize.Left, fullSize.Top);
      ClientSize =
      Size       = fullSize . Size;
   }


   /// <summary>
   /// This makes the window alteast semi-transparent
   /// </summary>
   /// <param name="TransparentToMouse">True if mouse clicks pass thru to lower layers</param>
   protected void Transparent(bool TransparentToMouse)
   {
      // This is needed since we're over the whole display, and we don't want the other
      // areas to be blurry
      TransparencyKey =  BackColor =ForeColor= System.Drawing.Color.Fuchsia;
      DoubleBuffered  = true;
      SetStyle(ControlStyles . OptimizedDoubleBuffer,        true);
      SetStyle(ControlStyles . SupportsTransparentBackColor, true);
      SetStyle(ControlStyles . AllPaintingInWmPaint,         true);
      SetStyle(ControlStyles . UserPaint,                    true);

      // This is need to pass mouse clicks thru to lower layers
      if (TransparentToMouse)
        ExStyle |= (int) WS . EX_TRANSPARENT;
   }


   #region Bits that make it not focusable
   internal const int MA_NOACTIVATE = 0x0003;
   protected override void WndProc(ref Message m)
   {
      if (m.Msg == (int) WM.MOUSEACTIVATE)
        {
           m.Result = (IntPtr) MA_NOACTIVATE;
           return;
        }
      if (m . Msg == (int) WM.FOCUS)
        {
           m . Result = (IntPtr)1;
           return;
        }
      base . WndProc(ref m);
   }

   protected override bool ShowWithoutActivation
   {
      get
      {
         return true;
      }
   }
   #endregion
}
